package cn.yilongquan.controller;

import cn.yilongquan.authorize.PreAuthorize;
import cn.yilongquan.service.IEmployeeService;
import cn.yilongquan.user.query.EmployeeQuery;
import cn.yilongquan.user.domain.Employee;
import cn.yilongquan.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "员工管理接口")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @ApiOperation(value = "根据ID查询员工")
    @GetMapping("/{id}")
    @PreAuthorize(sn = "employee:findById", name = "查询员工", descs = "根据ID查询员工详情")
    public R findById(@PathVariable Long id) {
        return R.ok(employeeService.findById(id));
    }

    @ApiOperation(value = "查询所有员工")
    @GetMapping
    @PreAuthorize(sn = "employee:findAll", name = "查询所有员工", descs = "查询系统所有员工")
    public R findAll() {
        return R.ok(employeeService.findAll());
    }

    @ApiOperation(value = "新增/修改员工")
    @PostMapping("/add")
    @PreAuthorize(sn = "employee:save", name = "保存员工", descs = "新增或修改员工信息")
    public R save(@RequestBody Employee employee) {
        if (employee.getId() == null) {
            employeeService.add(employee);
        } else {
            employeeService.update(employee);
        }
        return R.ok();
    }

    @ApiOperation(value = "根据ID删除员工")
    @DeleteMapping("/{id}")
    @PreAuthorize(sn = "employee:deleteById", name = "删除员工", descs = "根据ID删除员工")
    public R deleteById(@PathVariable Long id) {
        employeeService.deleteById(id);
        return R.ok();
    }

    @ApiOperation(value = "批量删除员工")
    @PostMapping("/deleteAll")
    @PreAuthorize(sn = "employee:batchDelete", name = "批量删除员工", descs = "批量删除选中员工")
    public R batchDelete(@RequestBody List<Long> ids) {
        employeeService.batchDelete(ids);
        return R.ok();
    }

    @ApiOperation(value = "分页查询员工")
    @PostMapping("/list")
    @PreAuthorize(sn = "employee:pageList", name = "分页查询员工", descs = "分页+条件查询员工")
    public R pageList(@RequestBody EmployeeQuery query) {
        return R.ok(employeeService.pageList(query));
    }

    @ApiOperation(value = "根据员工ID查询角色ID列表")
    @GetMapping("/roleIds/{employeeId}")
    public R findRoleIdsByEmployeeId(@PathVariable Long employeeId) {
        return R.ok(employeeService.findRoleIdsByEmployeeId(employeeId));
    }
}